/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.applybook.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.dao.gen.bean.BizApplyBook;
import com.nmf.dao.gen.bean.BizApplyPeriod;
import com.nmf.site.biz.applybook.enums.BookStatusEnums;
import com.nmf.site.biz.applybook.service.BizApplyBookService;
import com.nmf.site.biz.applyperiod.service.BizApplyPeriodService;
import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.modules.sys.utils.UserUtils;

/**
 * 申请书Controller
 * @author bm
 * @version 2019-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/applybook/bizApplyBook")
public class BizApplyBookController extends BaseController {

	@Autowired
	private BizApplyBookService bizApplyBookService;
	@Autowired
	BizApplyPeriodService bizApplyPeriodService;
	@ModelAttribute
	public BizApplyBook get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizApplyBook entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizApplyBookService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizApplyBook();
		}
		return entity;
	}
	@RequestMapping(value = {"avlible/period/list"})
	public String periodlist(BizApplyPeriod bizApplyPeriod, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizApplyPeriod> page = bizApplyPeriodService.findPage(new Page<BizApplyPeriod>(request, response), bizApplyPeriod); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybook/bizApplyPeriodList";
		}
		return "biz/applybook/bizApplyPeriodList";
	}
	
	@RequestMapping(value = {"avlible/period/applybook"})
	public String applybook(BizApplyPeriod bizApplyPeriod, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(bizApplyBookService.isVal(bizApplyPeriod.getId())) {
			model.addAttribute("message", "已经申请，不可重复提交");
			return "biz/applybook/bizApplyPeriodList";
		}
		BizApplyPeriod dbBizApplyPeriod = bizApplyPeriodService.get(bizApplyPeriod.getId());
		
		//前端显示 传值
		BizApplyBook bizApplyBook = new BizApplyBook();
		bizApplyBook.setCategoryId(dbBizApplyPeriod.getCategoryId());
		bizApplyBook.setCategoryName(dbBizApplyPeriod.getCategoryName());
		bizApplyBook.setPeriodId(dbBizApplyPeriod.getId());
		bizApplyBook.setApplyType(Integer.parseInt(dbBizApplyPeriod.getApplyType()));
		model.addAttribute("bizApplyBook", bizApplyBook);
		return "biz/applybook/bizApplyBookForm";
	}
	
	@RequiresPermissions("applybook:bizApplyBook:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizApplyBook bizApplyBook, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizApplyBook> page = bizApplyBookService.findPage(new Page<BizApplyBook>(request, response), bizApplyBook); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybook/bizApplyBookList";
		}
		return "biz/applybook/bizApplyBookList";
	}
	
	@RequestMapping(value = "form")
	public String form(BizApplyBook bizApplyBook, Model model) {
		model.addAttribute("bizApplyBook", bizApplyBook);
		return "biz/applybook/bizApplyBookForm";
	}

	@RequiresPermissions("applybook:bizApplyBook:edit")
	@RequestMapping(value = "save")
	public String save(BizApplyBook bizApplyBook, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizApplyBook)){
			return form(bizApplyBook, model);
		}
		try{
			BizApplyPeriod dbBizApplyPeriod = bizApplyPeriodService.get(bizApplyBook.getPeriodId());
			bizApplyBook.setCategoryId(dbBizApplyPeriod.getCategoryId());
			bizApplyBook.setCategoryName(dbBizApplyPeriod.getCategoryName());
			bizApplyBook.setPeriodId(dbBizApplyPeriod.getId());
			bizApplyBook.setApplyType(Integer.parseInt(dbBizApplyPeriod.getApplyType()));
			bizApplyBook.setOptId(UserUtils.getUser().getId());
			bizApplyBook.setOptName(UserUtils.getUser().getName());
			
			bizApplyBookService.save(bizApplyBook);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "error/500";
		}
		model.addAttribute("message",  "申请书已经提交，请等待上级部门审核");
		return "success/success";
	}
	
	
	@RequiresPermissions("applybook:bizApplyBook:edit")
	@RequestMapping(value = "delete")
	public String delete(BizApplyBook bizApplyBook, RedirectAttributes redirectAttributes) {
		try{
			bizApplyBookService.delete(bizApplyBook);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/applybook/bizApplyBook/?repage";
		}
		addMessage(redirectAttributes, "删除申请书成功");
		return "redirect:"+Global.getAdminPath()+"/applybook/bizApplyBook/?repage";
	}

}